<?php

namespace App\Http\Controllers\Admin\Account;

use App\B2BProject\Admin\AdminControllerBuildTrait;
use App\B2BProject\Area;
use App\B2BProject\Custom\Member;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class AccountManagementController extends Controller
{
    use AdminControllerBuildTrait;

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        if ($request->ajax()) {

            $query = Member::orderBy('created_at', 'desc');

            if ($company = $request->query('company_name')) {
                $query->whereHas('information', function ($query) use ($company) {
                    $query->where('company', 'like', "%$company%");
                });
            }

            if ($username = $request->query('username')) {
                $query = $query->where('user', 'like', "%$username%");
            }

            $members = $query->paginate($request->query('pageSize', 7));

            $data = [];

            foreach ($members as $member) {
                $item   = [
                    'id'            => $member->id,
                    'user_name'     => $member->user,
                    'company_name'  => $member->information->company,
                    'total'         => '--',
                    'total'         => '--',
                    'may_money'     => '--',
                    'balance_money' => '--',
                    'close_money'   => '--',
                    'bank_name'     => '--',
                    'bank_num'      => '--',
                    'car_num'       => '--',
                    'account_name'  => '--',
                    'edit_url'      => route('admin.account.edit', [$member->id]),
                ];
                $data[] = $item;
            }

            return response()->json([
                'data'           => $data,
                'pagingConfData' => [
                    'page'     => $members->currentPage(),
                    'pageSize' => $request->query('pageSize'),
                    'total'    => $members->total(),
                ],
            ]);
        }

        return view('admin/account/index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
